package com.ruoyi.central.controller;

import java.util.List;

import com.ruoyi.central.service.ITopCentralArtProcessService;
import com.ruoyi.common.utils.service.ServiceUtils;
import com.ruoyi.common.validation.group.InsertGroup;
import com.ruoyi.common.validation.group.UpdateGroup;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.utils.bean.SelectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.central.domain.TopCentralArtProcessRun;
import com.ruoyi.central.service.ITopCentralArtProcessRunService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.validation.group.UpdateBatchGroup;
import com.ruoyi.common.core.domain.StateVo;
import java.util.Collection;
/**
 * 工艺流程执行Controller
 * 
 * @author ruoyi
 * @date 2022-03-02
 */
@Controller
@RequestMapping("/central/centralArtProcessRun")
public class TopCentralArtProcessRunController extends BaseController
{
    private String prefix = "central/centralArtProcessRun";

    @Autowired
    private ITopCentralArtProcessRunService topCentralArtProcessRunService;

    @Autowired
    private ITopCentralArtProcessService artProcessService;

    @RequiresPermissions("central:centralArtProcessRun:view")
    @GetMapping()
    public String centralArtProcessRun()
    {
        return prefix + "/centralArtProcessRun";
    }

    /**
     * 查询工艺流程执行列表
     */
    @RequiresPermissions("central:centralArtProcessRun:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(TopCentralArtProcessRun topCentralArtProcessRun)
    {
        startPage();
        List<TopCentralArtProcessRun> list = topCentralArtProcessRunService.selectList(topCentralArtProcessRun);
        ServiceUtils.complete(list,artProcessService,(artProcessMap,artProcessRun)->{
            if(artProcessMap.containsKey(artProcessRun.getArtProcessId())){
                artProcessRun.setArtProcess(artProcessMap.get(artProcessRun.getArtProcessId()));
            }
        });
        return getDataTable(list);
    }

    /**
     * 查询简单的工艺流程执行列表 用于select选择器
     */
    @GetMapping("/simpleList")
    @ResponseBody
    public AjaxResult simpleList(TopCentralArtProcessRun topCentralArtProcessRun){

        Collection<TopCentralArtProcessRun> topCentralArtProcessRunList = topCentralArtProcessRunService.selectAllMap().values();

        return AjaxResult.success(SelectUtils.buildSelectList(topCentralArtProcessRunList,TopCentralArtProcessRun::getId,TopCentralArtProcessRun::getProdBatchNo));
    }

    /**
     * 获取工艺流程执行详细信息
     */
    @RequiresPermissions("central:centralArtProcessRun:query")
    @GetMapping(value = "/{id}")
    @ResponseBody
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(topCentralArtProcessRunService.selectById(id));
    }

    /**
     * 获取自增排序值
     * @param topCentralArtProcessRun 过滤条件
     * @return
     */
    @GetMapping("/getIncrementWeight")
    @ResponseBody
    public AjaxResult getIncrementWeight(TopCentralArtProcessRun topCentralArtProcessRun){
        int weigth = topCentralArtProcessRunService.getMaxOfWeight(topCentralArtProcessRun);
        return AjaxResult.success(++weigth);
    }

    /**
     * 下载工艺流程执行导入模板
     */
    @GetMapping("/importTemplate")
    @ResponseBody
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<TopCentralArtProcessRun> util = new ExcelUtil<>(TopCentralArtProcessRun.class);
        util.importTemplateExcel(response, "工艺流程执行数据");
    }

    /**
     * 导入工艺流程执行数据
     * @param file 工艺流程执行数据文件
     * @param updateSupport 是否为更新
     */
    @Log(title = "工艺流程执行", businessType = BusinessType.IMPORT)
    @RequiresPermissions("central:centralArtProcessRun:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<TopCentralArtProcessRun> util = new ExcelUtil<>(TopCentralArtProcessRun.class);
        List<TopCentralArtProcessRun> topCentralArtProcessRunList = util.importExcel(file.getInputStream());

        if(updateSupport){
            if(topCentralArtProcessRunService.updateBatchById(topCentralArtProcessRunList)){
                return AjaxResult.success("导入工艺流程执行数据覆盖成功！");
            }
            return AjaxResult.error("导入工艺流程执行数据覆盖失败！");
        }
        if(topCentralArtProcessRunService.insertBatch(topCentralArtProcessRunList)){
            return AjaxResult.success("导入工艺流程执行数据新增成功！");
        }
        return AjaxResult.error("导入工艺流程执行数据新增失败！");
    }

    /**
     * 导出工艺流程执行列表
     */
    @RequiresPermissions("central:centralArtProcessRun:export")
    @Log(title = "工艺流程执行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TopCentralArtProcessRun topCentralArtProcessRun)
    {
        List<TopCentralArtProcessRun> list = topCentralArtProcessRunService.selectList(topCentralArtProcessRun);
        ExcelUtil<TopCentralArtProcessRun> util = new ExcelUtil<TopCentralArtProcessRun>(TopCentralArtProcessRun.class);
        return util.exportExcel(list, "工艺流程执行数据");
    }

    /**
     * 新增工艺流程执行
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存工艺流程执行
     */
    @RequiresPermissions("central:centralArtProcessRun:add")
    @Log(title = "工艺流程执行", businessType = BusinessType.INSERT)
    @PostMapping
    @ResponseBody
    public AjaxResult addSave(@Validated(InsertGroup.class) @RequestBody TopCentralArtProcessRun topCentralArtProcessRun)
    {
        return toAjax(topCentralArtProcessRunService.insert(topCentralArtProcessRun));
    }

    /**
     * 修改工艺流程执行
     */
    @RequiresPermissions("central:centralArtProcessRun:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap)
    {
        TopCentralArtProcessRun topCentralArtProcessRun = topCentralArtProcessRunService.selectById(id);
        mmap.put("topCentralArtProcessRun", topCentralArtProcessRun);
        return prefix + "/edit";
    }

    /**
     * 修改保存工艺流程执行
     */
    @RequiresPermissions("central:centralArtProcessRun:edit")
    @Log(title = "工艺流程执行", businessType = BusinessType.UPDATE)
    @PutMapping
    @ResponseBody
    public AjaxResult editSave(@Validated(UpdateGroup.class) @RequestBody TopCentralArtProcessRun topCentralArtProcessRun)
    {
        return toAjax(topCentralArtProcessRunService.updateById(topCentralArtProcessRun));
    }

    /**
     * 修改工艺流程执行 状态 根据 ids 主键
     */
    @RequiresPermissions("central:centralArtProcessRun:edit")
    @Log(title = "工艺流程执行", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStateByIds")
    @ResponseBody
    public AjaxResult updateStateByIds(@Validated(value= UpdateBatchGroup.class)@RequestBody StateVo<Integer> stateVo)
    {
        return toAjax(topCentralArtProcessRunService.updateStateByIds(stateVo.getIds(),stateVo.getState()));
    }

    /**
     * 恢复工艺流程执行 为未删除 根据 ids 主键
     */
    @RequiresPermissions("central:centralArtProcessRun:restore")
    @Log(title = "工艺流程执行", businessType = BusinessType.UPDATE)
    @PutMapping("/restoreData/{ids}")
    @ResponseBody
    public AjaxResult recoverByIds(@PathVariable("ids") List<Integer> ids){
        return toAjax(topCentralArtProcessRunService.recoverBatchByIds(ids));
    }

    /**
     * 删除工艺流程执行
     */
    @RequiresPermissions("central:centralArtProcessRun:remove")
    @Log(title = "工艺流程执行", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ResponseBody
    public AjaxResult deleteByIds(@PathVariable("ids") List<Integer> ids)
    {
            return toAjax(topCentralArtProcessRunService.logicDeleteBatchByIds(ids));
    }
}
